package com.hmdp.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.hmdp.dto.Result;
import com.hmdp.entity.ShopType;
import com.hmdp.mapper.ShopTypeMapper;
import com.hmdp.service.IShopTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

import static com.hmdp.utils.RedisConstants.CACHE_SHOPTYPE_KEY;

/*
* @Description: TODO
* @Author: YangTing
* @Date: 2023/5/22 15:40
**/
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

//    String方式实现
    @Override
    public Result getTypeList() {
        String shopTypeStr = stringRedisTemplate.opsForValue().get(CACHE_SHOPTYPE_KEY);
        if (StrUtil.isNotBlank(shopTypeStr)) {
            List<ShopType> shopType = JSONUtil.toList(shopTypeStr,ShopType.class);
            return Result.ok(shopType);
        }
        List<ShopType> shopTypeList = query().orderByAsc("sort").list();
        stringRedisTemplate.opsForValue().set(CACHE_SHOPTYPE_KEY,JSONUtil.toJsonStr(shopTypeList));
        return Result.ok(shopTypeList);
    }

//        @Override
//    public Result getTypeList() {
//        List<String> shopType = stringRedisTemplate.opsForList().range(CACHE_SHOPTYPE_KEY, 0, -1);
//        if (shopType.size() != 0) {
//            return Result.ok(shopType);
//        }
//        List<ShopType> shopTypeList = query().orderByAsc("sort").list();
//        stringRedisTemplate.opsForList().rightPushAll(CACHE_SHOPTYPE_KEY,JSONUtil.toJsonStr(shopTypeList));
//        return Result.ok(shopTypeList);
//    }
}
